package com.xiaoliang.servlet;

import com.xiaoliang.entry.Product;
import com.xiaoliang.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author XIAOLIANG
 * @ClassName AddProduct.java
 * @Description TODO
 * @createTime 2021年02月26日 09:48:00
 */
//@WebServlet("/product/addProduct")
public class AddProduct extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name").trim();
		String price = req.getParameter("price").trim();
		String count = req.getParameter("count").trim();
		String description = req.getParameter("description").trim();

		ProductServiceImpl productService = new ProductServiceImpl();


		boolean b = productService.addProduct(new Product(name, Double.parseDouble(price), Integer.parseInt(count), 0, description));

		if(b){

			System.out.println("添加成功!");
			req.getSession().setAttribute("msg","添加成功!");

		}else {

			System.out.println("添加失败");
			req.getSession().setAttribute("msg","添加失败!");
		}
		resp.sendRedirect(req.getContextPath()+"/product/addOrUpdate.jsp");

	}
}
